package personal.xjl._23java2studentdemo.mapper;

import org.apache.ibatis.annotations.*;
import personal.xjl._23java2studentdemo.model.Course;
import personal.xjl._23java2studentdemo.model.Student;
import personal.xjl._23java2studentdemo.model.Teacher;

import java.util.List;

@Mapper
public interface TeacherMapper {
    @Select("select * from teacher ")
    @Results({
            @Result(column = "course_id",property = "course",javaType = Course.class,
                    one = @One(select = "personal.xjl._23java2studentdemo.mapper.CourseMapper.selectById"))
    })
    List<Teacher> selectAll();

    //删除教师
    @Delete("delete from teacher where id=#{id}")
    public int deleteById(int id);

    //新增教师
    @Insert("insert into teacher(name,password,sex,birthday,course_id,professional) values (#{name},#{password},#{sex},#{birthday},#{course_id},#{professional})")
    int insert(Teacher teacher);

    //修改
    @Update("update teacher set name=#{name},password=#{password},sex=#{sex},birthday=#{birthday},course_id=#{course_id},professional=#{professional} where id=#{id}")
    int update(Teacher teacher);

    @Select("select * from teacher where id = #{id}")
    Teacher selectById(int id);

    @Select(("select * from teacher limit #{fromIndex},#{pageSize}"))
    @Results({
            @Result(column = "course_id",property = "course",javaType = Course.class,
                    one = @One(select = "personal.xjl._23java2studentdemo.mapper.CourseMapper.selectById"))
    })
    List<Teacher> selectByPage(int fromIndex, int pageSize);

    @Select("SELECT * FROM teacher WHERE name=#{username} AND (password IS NULL OR password='' OR password=#{password})")
    Teacher selectByNameAndPwd(String username, String password);

}
